/*
 * jQuery submitConfirm plugin
 * Copyright (c) 2011 TMLA INC
 * Written by Dwight Brown as a gift to Tim Gallagher
 * This plugin confirms a form submission using jQuery ui dialogs.
 * Version: 0.2 2011-12-16
 * Changelog:
 * 0.2 - 2011-12-16 - bug fix for anchor elements not working
 * 0.1 - 2011-12-02 - initial release

	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

;(function ( $, window, document, undefined )
{
	function fireEvent( obj, event )
	{
		var fireOnThis = obj;
		if( document.createEvent )
		{
			var eventObj = document.createEvent( 'MouseEvents' );
			eventObj.initEvent( event, true, false );
			fireOnThis.dispatchEvent( eventObj );
		}
		else if( document.createEventObject )
		{
			var eventObj = document.createEventObject();
			fireOnThis.fireEvent( 'on' + event, eventObj );
		}
		else
		{
			// throw exception
		}
	}

    var pluginName = "submitConfirm",
		defaults =
		{
		    html: "Are you sure you want to proceed?",
		    title: "Please Confirm",
			confirmButtonLabel: 'Yes',
			cancelButtonLabel: 'No',
			beforeShow: function() { },
			onConfirm: function( event ) { return true },
			onCancel: function( event ) {}
		};

    var Plugin = function( element, options )
    {
		var confirmed = false;
		var isForm = $(element).is( 'form' );
		var elementEvent = (isForm?'submit':'click');

    	var opts = $.extend( {}, defaults, options );

	    var dialog = $( '<div style="display:none;">' )
		.html( opts.html )
		.dialog(
        {
			open: function()
			{
				opts.beforeShow.call( element, dialog );
			},
            modal: true,
            title: opts.title,
            autoOpen: false,
			buttons :
			[
			   {
			        text: opts.confirmButtonLabel,
			        click: function( event )
			        {
						confirmed = opts.onConfirm.call( this, event );
						dialog.dialog( "close" );
						if ( confirmed )
						{
							fireEvent( element, elementEvent );
						}
			        }
			    },
			    {
					text: opts.cancelButtonLabel,
					click: function( event )
					{
						dialog.dialog( "close" );
						confirmed = false;
						opts.onCancel.call( this, event );
					}
				}
			]
		});

		$(element).bind( elementEvent,
		function( event )
        {
			if ( ! confirmed )
			{
				dialog.dialog( "open" );
				event.preventDefault();
			}
        });
	};

    // Init the plugin on all elements
    $.fn[pluginName] = function( options )
    {
        return this.each(function ()
        {
            // Prevent re-instantiation
            if ( !$.data(this, 'plugin_' + pluginName) )
            {
                $.data(this, 'plugin_' + pluginName,
                new Plugin( this, options ));
            }
        });
    };

})( jQuery, window, document, undefined );